#region Summary

// 
// Owner : JackieHan 
// 
// Solution : WebShopApi 
// 
// Project : Jackie.Utility
// 
// File : JackieMath.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 自定义数据运算
// 
// History : 
//			2011-10-22 19:34:59	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;

#endregion

namespace Jackie.Utility
{
    ///<summary>
    ///自定义数据运算
    ///</summary>
    public class JackieMath
    {

        /// <summary>
        /// 去小数（四舍五入）
        /// </summary>
        /// <param name="d">小数</param>
        /// <returns></returns>
        public static decimal Round(decimal d)
        {
            string dStr = d.ToString();
            int index = dStr.IndexOf(".");
            if (index != -1)
            {
                string decimalFractionStr = dStr[index + 1].ToString();
                int decimalFraction = int.Parse(decimalFractionStr);
                return decimalFraction >= 5
                           ? decimal.Parse(dStr.Substring(0, index)) + 1
                           : decimal.Parse(dStr.Substring(0, index));
            }
            return d;
        }

        /// <summary>
        /// 保留小数
        /// </summary>
        /// <param name="d">数值</param>
        /// <param name="decimals">小数位数</param>
        /// <returns></returns>
        public static decimal Round(decimal d, int decimals)
        {
            if (d >= 0)
            {
                d += (decimal)(5 * Math.Pow(10, -(decimals + 1)));
            }
            else
            {
                d += (decimal)(-5 * Math.Pow(10, -(decimals + 1)));
            }
            string str = d.ToString();
            string[] strs = str.Split('.');
            int idot = str.IndexOf('.');
            string prestr = strs[0];
            string poststr = strs[1];
            if (poststr.Length > decimals)
            {
                poststr = str.Substring(idot + 1, decimals);
            }
            string strd = prestr + "." + poststr;
            d = decimal.Parse(strd);
            return d;
        }
    }
}